import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { visualizer } from 'rollup-plugin-visualizer'
import viteImagemin from 'vite-plugin-imagemin'
// import { viteStaticCopy } from 'vite-plugin-static-copy'
// 不要在生产环境使用 alpha 版本依赖

// https://vite.dev/config/
export default defineConfig(({ mode }) => ({
  plugins: [
    vue(),
    vueJsx(),
    mode === 'development' ? vueDevTools() : null,
    AutoImport({
      dts: 'src/auto-imports.d.ts',
      imports: ['vue', 'vue-router', 'pinia'],
      resolvers: [ElementPlusResolver()],
      eslintrc: {
        enabled: true,
      },
    }),
    Components({
      resolvers: [ElementPlusResolver()],
      dts: 'src/components.d.ts',
    }),
    visualizer({
      open: true,
      gzipSize: true,
      brotliSize: true,
    }),
    viteImagemin({
      gifsicle: { optimizationLevel: 7 },
      mozjpeg: { quality: 20 },
      optipng: { optimizationLevel: 7 },
      pngquant: { quality: [0.8, 0.9] },
      svgo: {
        plugins: [{ name: 'removeViewBox' }, { name: 'removeEmptyAttrs', active: false }],
      },
    }),
    // viteStaticCopy({
    //   targets: [
    //     {
    //       src: 'src/i18n',
    //       dest: '',
    //     },
    //   ],
    // }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  base: './',
  optimizeDeps: {
    include: ['nprogress', 'nprogress/nprogress.css'],
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    manifest: true,
    assetsInclude: ['**/*.json'], // 包含JSON文件作为静态资源
    copyPublicDir: true, // 确保public目录内容被复制
    assetsInlineLimit: 4096,
    rollupOptions: {
      output: {
        manualChunks: {
          'vue-vendor': ['vue', 'vue-router', 'pinia'], // 这里这段代码必须要放到前边，不然打包后报错
          'element-plus': ['element-plus'],
        },
      },
    },
    minify: false, // 禁用压缩
    sourcemap: true, // 生成sourcemap
  },
}))
